package com.zprmb.udzt;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.MyReceiver;
import com.zprmb.udzt.util.VolleryUtil;

public class Local_address extends BaseActivity implements OnClickListener{
	private TextView tv_name,tv_tel,tv_address,tv_detail,tv_update;
	private RelativeLayout rl_delete,rl_default,rl_back;
	private String province,city,area,code;
	private ImageView img_back;
	private LinearLayout li_update;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.local_address);
		IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("exit");       
        this.registerReceiver(myReceiver,mFilter);
		tv_name=(TextView)findViewById(R.id.name);
		rl_delete=(RelativeLayout)findViewById(R.id.rl_delete);
		tv_tel=(TextView)findViewById(R.id.tel);
		li_update=(LinearLayout)findViewById(R.id.li_update);
		tv_address=(TextView)findViewById(R.id.address);
		tv_detail=(TextView)findViewById(R.id.deatil_address);
		tv_name.setText(getIntent().getStringExtra("name"));
		tv_tel.setText(getIntent().getStringExtra("tel"));
		img_back=(ImageView)findViewById(R.id.img_back);
		tv_address.setText(getIntent().getStringExtra("address"));
		tv_detail.setText(getIntent().getStringExtra("detail"));
		rl_default=(RelativeLayout)findViewById(R.id.rl_default);
		tv_update=(TextView)findViewById(R.id.tv_update);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		province=getIntent().getStringExtra("province");
		city=getIntent().getStringExtra("city");
		area=getIntent().getStringExtra("area");
		li_update.setOnClickListener(this);
		img_back.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		tv_update.setOnClickListener(this);
		rl_delete.setOnClickListener(this);
		rl_default.setOnClickListener(this);
		
	}
	 MyReceiver myReceiver = new MyReceiver(){
	        @Override
	        public void onReceive(Context context, Intent intent) {
	            finish();
	        }
	    };
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent=null;
		switch(v.getId()){
		case R.id.rl_delete:
			delete();
			break;
		case R.id.rl_default:
			setdefault();
			break;
		case R.id.li_update:
			 intent=new Intent(Local_address.this,Add_address.class);
			intent.putExtra("update", 1);
			intent.putExtra("id", getIntent().getStringExtra("id"));
			intent.putExtra("name", getIntent().getStringExtra("name"));
			intent.putExtra("tel", getIntent().getStringExtra("tel"));
			intent.putExtra("address", getIntent().getStringExtra("address"));
			intent.putExtra("province",province);
			intent.putExtra("city",city);
			intent.putExtra("area",area);
			intent.putExtra("detail",getIntent().getStringExtra("detail"));
			startActivity(intent);
			break;
			
		case R.id.tv_update:
			 intent=new Intent(Local_address.this,Add_address.class);
			intent.putExtra("update", 1);
			intent.putExtra("id", getIntent().getStringExtra("id"));
			intent.putExtra("name", getIntent().getStringExtra("name"));
			intent.putExtra("tel", getIntent().getStringExtra("tel"));
			intent.putExtra("address", getIntent().getStringExtra("address"));
			intent.putExtra("province",province);
			intent.putExtra("city",city);
			intent.putExtra("area",area);
			intent.putExtra("detail",getIntent().getStringExtra("detail"));
			startActivity(intent);
			break;
		case R.id.rl_back:
			finish();
			break;
		case R.id.img_back:
			finish();
			break;
		}
		
	}
	private void setdefault() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		params.put("addressId", getIntent().getStringExtra("id"));
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("默认", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					Intent intent=new Intent(Local_address.this,My_address.class);
					
					startActivity(intent);
				}else {
					
					if (jsonObject.optString("comment").equals("please login")) {
						
						
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(Local_address.this,Log_or_RegisActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}else{
							
							Intent intent = new Intent(Local_address.this,LoginActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}
						
					}else{
						Toast.makeText(Local_address.this, jsonObject.optString("comment"), 2000).show();
						Local_address.this.finish();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(Local_address.this, Constant.URL+"/api/v5/user/defaultAdr?", params.toString());
		
	}
	private void delete() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		params.put("addressId", getIntent().getStringExtra("id"));
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("delete", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					finish();
				}else {
					
					if (jsonObject.optString("comment").equals("please login")) {
						
						
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(Local_address.this,Log_or_RegisActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}else{
							
							Intent intent = new Intent(Local_address.this,LoginActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}
						
					}else{
						Toast.makeText(Local_address.this, jsonObject.optString("comment"), 2000).show();
						Local_address.this.finish();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(Local_address.this, Constant.URL+"/api/v5/user/deleteAdr?", params.toString());
		
	}

}
